<template>
	<div class="aid">
		<div class="base">
			<p class="fixed-top">
				{{ JSON.stringify(text) == '{}' ? '添加假期社会实践审批权限' : '修改假期社会实践审批权限' }}
				<a-icon @click="closeFunction" type="close" />
			</p>
			<div class="main" id="new_message">
				<a-form :form="form">
					<a-form-item label="类型"  >
						<a-select @change="selectChange" optionFilterProp="children" showSearch v-decorator="['FlowType',{ rules: [{ required: true, message: '请选择类型' }] }]">
							<template v-for="(item, index) in PracticeTeamData">
								<a-select-option :key="item.ID">{{ item.Name }}</a-select-option>
							</template>
						</a-select>
					</a-form-item>
					<a-form-item label="维度">
						<a-select v-decorator="['Type', { rules: [{ required: true, message: '请选择维度' }] }]" @change="typeChange">
							<a-select-option v-for="(item, i) in RangeTypeData" :value="item.Key">{{ item.Value }}</a-select-option>
						</a-select>
					</a-form-item>
					<a-form-item label="部门" v-if="form.getFieldValue('Type')==2">
						<a-select  :allowClear="true" 
						v-decorator="['DepartCode',{ rules: [{ required: true, message: '请选择部门' }] }]"
						placeholder="请选择部门" optionFilterProp="children" showSearch>
							<a-select-option v-for="(item, index) in DepartCodeList" :key="index" :value="item.Code">{{ item.Name }}</a-select-option>
						</a-select>
					</a-form-item>
					<a-form-item label="实践类型" v-if="form.getFieldValue('Type')==1">
							<a-select :allowClear="true" v-decorator="['PracticeTeamTypeID',{ rules: [{ required: true, message: '请选择实践类型' }] }]" placeholder="请选择实践类型"
								optionFilterProp="children" showSearch>
								<a-select-option v-for="(item, index) in TeamData" :key="index" :value="item.ID">
									{{ item.Name }}
								</a-select-option>
							</a-select>
						</a-form-item>
					<a-form-item label="审批节点" >
						<a-select v-decorator="['PracticeTeamFlowTypeID',{ rules: [{ required: true, message: '请选择审批节点' }] }]"  show-search :filter-option="filterOption"
							option-filter-prop="children" :autoClearSearchValue="false">
							<template v-for="(item, index) in ActivityFlowTypeList">
								<a-select-option :key="item.ID">{{ item.Name }}</a-select-option>
							</template>
						</a-select>
					</a-form-item>
					<a-form-item class="fixed-bottom">
						<a-button type="primary" @click="handleSubmit">{{ JSON.stringify(text) == '{}' ? '确认添加' : '确认修改' }}</a-button>
						<a-button style="margin-left: 10px;" @click="closeFunction">取消</a-button>
					</a-form-item>
				</a-form>
			</div>
		</div>
	</div>
</template>

<script>
import moment from 'moment';
import { ManageRolePracticeTeamFlowDoAdd, CommonDoGetPracticeTeamFlowTypeTypeList,ManageRolePracticeTeamFlowDoUpdate, GetClubPageListCommon,CommonDoGetCreditFlowTypeList,GetActivityTypeList } from '@/api/follow';
export default {
	props: {
		text: Object,
		TeamData:Array,
		DepartCodeList: Array,
		TypeIDList: Array,
		ClubList:Array
	},
	watch: {
		text: function(text) {
			if (!text.ID) {
				this.currentID = '';
			} else {
				let that = this;
				that.currentID = text.ID;
				that.getCommonDoGetPracticeTeamFlowTypeTypeList(text.FlowType)
				this.$nextTick(() => {
					setTimeout(function() {
						that.form.setFieldsValue({
							Type:text.Type,
							FlowType:text.FlowType,
							DepartCode:text.DepartCode,
							PracticeTeamFlowTypeID:text.PracticeTeamFlowTypeID,
							PracticeTeamTypeID:text.PracticeTeamTypeID,
						});
					}, 300);
				})
				this.$nextTick(() => {
					setTimeout(function() {
						that.form.setFieldsValue({
							DepartCode:text.DepartCode,
							PracticeTeamTypeID:text.PracticeTeamTypeID,
						});
					}, 1000);
				})
				console.log(text);
			}
		}
	},
	data() {
		return {
			PracticeTeamData:this.global.PracticeTeamData,
			ActivityTypeApplyList:[],
			ActivityFlowTypeList:[],
			replaceFields: {
				children: 'children',
				title: 'title',
				key: 'ID',
				value: 'ID'
			},
			loading: false,
			formLayout: 'horizontal',
			form: this.$form.createForm(this, {
				name: 'coordinated'
			}),
			currentID: '',
			DimensionList: this.global.DimensionList,
			RangeTypeData:[{
				Key:1,
				Value:"实践类型"
			},
			{
				Key:2,
				Value:"部门"
			}
			],
			CreditFlowTypeData:[],
			ActivityTypeData:[],
			// ClubList: [],
			typeID:'',
			FlowTypeList:this.global.activeFlowTypeList
		};
	},
	created() {
		this.getOther()
	},
	methods: {
		toTree(data) {
			let result = [];
			let map = {};
			if (!Array.isArray(data)) {
				return [];
			}
			data.forEach(item => {
				map[item.ID] = item;
			});
			data.forEach(item => {
				let parent = map[item.ParentID];
				if (parent) {
					(parent.children || (parent.children = [])).push(item);
				} else {
					result.push(item);
				}
			});
			return result;
		},
		async getOther(){
			// let res = await GetActivityTypeList();
			// this.ActivityTypeData = res.data.data;
			// this.ActivityTypeApplyList= res.data.data
			// this.ActivityTypeApplyList = this.ActivityTypeApplyList.map(item => {
			// 	return {
			// 		...item,
			// 		title: item.Name,
			// 		// disabled: item.IsApply ? false : true
			// 	};
			// });
			// this.ActivityTypeApplyList = this.toTree(this.ActivityTypeApplyList);
			// console.log(this.ActivityTypeApplyList);
			// let res1 = await CommonDoGetCreditFlowTypeList();
			// this.CreditFlowTypeData = res1.data.data;
			
		},
		moment,
		filterOption(input, option) {
			return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
		},
		typeChange(e){
			this.typeID = e
		},
		closeFunction(data) {
			this.form.resetFields();
			this.$emit('closeFun', data);
		},
		selectChange(e){
			this.getCommonDoGetPracticeTeamFlowTypeTypeList(e)
		},
		async getCommonDoGetPracticeTeamFlowTypeTypeList(type){
			
			let res = await CommonDoGetPracticeTeamFlowTypeTypeList({type:type,isTeacherExam:false});
			console.log(res)
			this.ActivityFlowTypeList = res.data.data;
			console.log(this.ActivityFlowTypeList)
		},
		async fetchClub(value) {
			let myReg = /^[^a-zA-Z]+$/;
			if (value && value.length>1) {
				let data = {};
				data.key = value;
				let res = await GetActivityTypeList(data);
				this.ActivityTypeData = res.data.data;
			}
		},
		handleSubmit(e) {
			let that = this;
			this.form.validateFields(async (err, values) => {
				if (!err) {
					let data = {};
					data=values;
					data.RoleID = that.$route.query.roleID
					data.id = this.currentID
					let res = {};
					
						// let list =[{}]
						// for(let i=0;i<list.length;i++){
							// data.RangeType = 2
							// data.CreditFlowTypeID=values.CreditFlowTypeID
							// data.DepartCode =values.DepartCode
							res = !this.currentID?await ManageRolePracticeTeamFlowDoAdd(data):await ManageRolePracticeTeamFlowDoUpdate(data);
							if(res.data.code==0){
								that.$message.success(res.data.msg);
							}else{
								that.$message.error(res.data.msg);
							}
						//}
					
					
					this.closeFunction('1');
				}
			});
		}
	}
};
</script>

<style lang="less" scoped>
/deep/.ant-form {
	margin-top: 50px;
	padding-bottom: 50px;
}
/deep/.ant-form-item {
	margin: 20px 50px;
	display: flex;
}
/deep/.ant-form-item-label {
	width: 100px;
	overflow: hidden;
	white-space: nowrap;
	text-overflow: ellipsis;
	text-align: right;
}
/deep/.ant-form-item-control-wrapper {
	flex: 1;
}
/deep/.ant-calendar-picker {
	width: 100% !important;
}
.ant-upload-list-picture-card-container {
	width: 80px;
	height: 80px;
}
</style>
